
#include <stdio.h>
#include "GL/glew.h"

#include "lg_draw.h"
#include "lg_shader.h"
#include "lg_material.h"

#define LGOGL_CORE

#include "lgogl_draw.h"
#include "lgogl_canvas.h"
#include "lgogl_vertex.h"
#include "lgogl_shader.h"

static struct {
	LGDepthTest depthTest;
}ctx = { 0 };

static void
bindDraw(LGCanvas* canvas, LGDraw* draw) {
	lgoglBindCanvas(canvas);
	lgoglBindShaderDraw(canvas, draw);
	lgoglBindVertex(draw->vertex);
}

static void
drawPoints(LGCanvas* canvas, LGFloat size, LGDraw* draw){
	bindDraw(canvas, draw);
	glPointSize((GLfloat)size);
	void* offset = (void*)(sizeof(unsigned) * draw->offset);
	glDrawElements(GL_POINTS,
		draw->count, GL_UNSIGNED_INT, offset);
}

static void
draw(LGCanvas* canvas, LGDraw* draw) {
	bindDraw(canvas, draw);
	LGMaterial* material = draw->material;
	GLint type = LG_DRAW_TRIANGLES;
	switch (material->drawType) {
	case LG_DRAW_POINTS:
		type = GL_POINTS; break;
	case LG_DRAW_LINES:
		type = GL_LINES; break;
	case LG_DRAW_LINE_STRIP:
		type = GL_LINE_STRIP; break;
	case LG_DRAW_LINE_LOOP:
		type = GL_LINE_LOOP; break;
	case LG_DRAW_TRIANGLES:
		type = GL_TRIANGLES; break;
	case LG_DRAW_TRIANGLE_STRIP:
		type = GL_TRIANGLE_STRIP; break;
	case LG_DRAW_TRIANGLE_FAN:
		type = GL_TRIANGLE_FAN; break;
	}
	void* offset = (void*)(sizeof(unsigned) * draw->offset);
	glDrawElements(type,
		draw->count, GL_UNSIGNED_INT, offset);
}


static LGDrawBackend backend = {
	drawPoints,
	draw,
};

LGOGL_API LGDrawBackend*
lgoglDrawBackend(){
    return &backend;
}

LGOGL_API int
lgoglInitDrawContext(LGConfig* config, LGOGLConfig* oglConfig) {
	return 0;
}